
var express = require('express');
const jwt=require('jsonwebtoken')
const UserModel = require('../../modules/user');
const md5=require('md5')
var router = express.Router();


/* GET home page. */

router.get('/login', (req, res)=>{
    res.render('login')
});
router.post('/login', (req, res)=>{
    const {username,password}=req.body
    UserModel.find({$and:[{username:username},{password:md5(password)}]}).then(response=>{
       if(response.length!==0){
        let token=jwt.sign({
            username:username},'atguigu',{expiresIn:6000})
        
            res.json({
                code:'200',
                msg:'登录成功',
                data:token
            })
       }else{
       res.json({
        code:'404',
        msg:'数据读取失败！',
        data:null
       })
       }
    }).catch(error=>{
        console.log(error.message)
    })
});


module.exports = router;
